const {
  $Toast
} = require('../../../utils/iview/base/index');
const Tools = require('../../../utils/modules/Tools');
import NetWork from '../../../utils/modules/NetWork';

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressId: null,
    casArray: ['微信支付'],
    casIndex: 0,
    _baseUrl: app.globalData._baseUrl,
    token: null,
    allPrice: 0,
    userAddress: null,//用户的收货地址
    orderList: null,//请求的订单列表
    goods_spec: null,//用户选中规格
    pay_status: null,//订单状态
    goods_id: null,//商品id
    address_id: null,//用户地址id
    remark: null,//订单备注
    goods: null,
    order_number: null,//订单编号
    cart_id: null,//购物车数组id
  },
  onLoad: function (options) {
    var _that = this;
    var token = wx.getStorageSync('userInfo');
    _that.addressInfo();
    _that.setData({
      token: token.access_token,
    })
  },
  onShow: function () {
    var _that = this;
    var userAddress = wx.getStorageSync('address');
    var userAddressId = userAddress.id;
    _that.setData({
      address_id: userAddressId,
    })
    if (userAddressId != '') {
      _that.addressChange(userAddressId);
    }
  },
  //提交订单
  placeGoods: function () {
    // console.log("提交了")
    // console.log(this.data.token);
    // console.log(this.data.goods);
    // console.log(this.data.address_id);
    // console.log(this.data.remark);
    let _that = this;
    if (!_that.data.address_id) {
      $Toast({
        content: '请先选择地址',
        type: 'warning'
      });
      return
    }
    let net = new NetWork(_that.data._baseUrl)
    var data = { token: _that.data.token, goods: _that.data.goods, address_id: _that.data.address_id, remark: _that.data.remark };
    net._request({
      url: '/api/v1/order/place',
      data: data,
      method: 'POST',
    }, '', res => {
      $Toast({
        content: '支付中，请稍后!',
        type: 'loading'
      });
      let _data = res.data.data;
      console.log(_data)
      _that.setData({
        order_number: _data.order_number,
      })
      net._request({
        url: 'api/v1/order/pay',
        data: {
          token: _that.data.token,
          order_number: _that.data.order_number,
        },
        method: 'POST'
      }, '', res => {
        let datas = res.data.data;
        console.log(res)
        console.log(datas)
        if (datas.result_code == "PAY_SUCCESS" && datas.return_code == 'SUCCESS') {
          wx.requestPayment({
            timeStamp: res.data.data.timeStamp,
            nonceStr: res.data.data.nonceStr,
            package: res.data.data.packages,
            signType: res.data.data.signType,
            paySign: res.data.data.paySign,
            success(res) {
              console.log(res)
              wx.navigateTo({
                url: 'pages/user/orderList/orderList',
              })
            },
            fail(res) { }
          })
        } else {
          $Toast({
            content: datas.return_msg,
            type: 'error'
          });
        }

        // _that.setData({
        //   orderList: datas
        // })
        // $Toast({
        //   content: datas.msg,
        //   type: 'success'
        // });
      })
    });
  },
  //订单备注
  valueToNum: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },
  //选择支付方式
  bindCasPickerChange(e) {
    this.setData({
      casIndex: e.detail.value
    })
  },
  // 获取用户默认地址
  addressInfo() {
    let _that = this;
    let net = new NetWork(_that.data._baseUrl)
    net._request({
      url: 'api/v1/member/address',
      data: {
        token: _that.data.token
      },
      method: 'POST'
    }, '', res => {
      let _data = res.data.data;
      _that.setData({
        userAddress: _data
      })
    });

  },
  //用户切换地址
  addressChange(id) {
    let _that = this;
    let net = new NetWork(_that.data._baseUrl)
    net._request({
      url: '/api/v1/member/address/show',
      data: {
        token: _that.data.token,
        id: id
      },
      method: 'POST'
    }, '', res => {
      let _data = res.data.data;
      _that.setData({
        userAddress: _data
      })
      console.log(_that.data.userAddress)
    });

  },
  //获取商品详情
  // requestGoodsInfo() {
  //   let _that = this;
  //   let net = new NetWork(_that.data._baseUrl)
  //   net._request({
  //     url: '/api/v1/goods/show/38',
  //     method: 'GET'
  //   }, '', res => {
  //     // console.log(res.data.data);
  //     var data = res.data.data;
  //     var arr = [];
  //     for (var i = 0; i < data.goods_spec.length; i++) {
  //       var multi = data.goods_spec[i].multi, index = { ids: 0, arr: [] };
  //       for (var j = 0; j < multi.length; j++) {
  //         var o = { name: multi[j], level: i, id: j, ids: 0 };
  //         index.arr.push(o);
  //       }
  //       arr.push(index)
  //     }
  //     _that.setData({
  //       orderList: data,
  //       goods_dsc: arr,
  //     });
  //   });
  // },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})